import 'package:flutter/material.dart';
import 'dart:async';

import 'package:beijingbus/pages/city/detail.dart';

class CityPage extends StatefulWidget {
  @override
  _CityPageState createState() => _CityPageState();
}

class _CityPageState extends State<CityPage> {
  Timer _timer;
  int _currentTime = 0;
  String _cityName = '';

  @override
  void initState() {
    super.initState();

    // 循环执行，间隔时间100毫秒
    _timer = Timer.periodic(Duration(milliseconds: 100), (timer) {
      _currentTime += 100;

      // 如果等于5，终止定时器
      if (_currentTime >= 3000) _timer.cancel();
      setState(() {});
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  // 倒计时
  Stack countDown() {
    return Stack(
      alignment: Alignment.center,
      children: [
        // 圆形进度
        CircularProgressIndicator(
          strokeWidth: 100.0,
          value: _currentTime / 3000,
        ),
        Text(
          '${3 - _currentTime ~/ 1000}',
          style: TextStyle(fontSize: 50.0, color: Colors.white),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    double boxHeight = 0.0;

    if (_currentTime ~/ 1000 < 3) {
      boxHeight = 200.0;
    } else {
      boxHeight = 0.0;
    }

    return Scaffold(
      appBar: AppBar(
        title: Text(_cityName),
      ),
      body: Container(
        padding: EdgeInsets.only(top: boxHeight),
        color: Colors.black,
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Column(
          children: [
            _currentTime ~/ 1000 < 3 ? countDown() : DetailPage(),
          ],
        ),
      ),
    );
  }
}
